var t = getApp(),
  e = t.requirejs("core");
Page({
  data: {
    order_no: '',
    show: true,
    detail: {},
    type: '',
    finished: false,
    wrong: false,
    time: ''
  },

  onLoad: function (options) {
    //e.setting();
    this.setData({
      order_no: options.order_no,
      config: getApp().config
    })
  },

  onShow: function () {
    this.orderDetail();
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 跳转商品详情
   */
  toGoodDetail: function (event) {
    var good_id = e.pdata(event).id;

    if (this.data.type == 't') {
      e.jump('/yb_mingpian/pages/goods/detail/index?id='+good_id, 1);
    }

    if (this.data.type == 'k') {
      e.jump('/yb_mingpian/pages/kanjia/goods_info/index?id='+good_id, 1);
    }

    if (this.data.type == 'p') {
      e.jump('/yb_mingpian/pages/pintuan/pages/goods/detail?gid='+good_id, 1);
    }

    if (this.data.type == 'm') {
      e.jump('/yb_mingpian/pages/miaosha/seckillGoods/index?id='+good_id, 1);
    }
  },

  /**
   * 获取订单信息
   */
  orderDetail: function () {
    var that = this;
    var order_no = that.data.order_no;
    order_no = order_no.substr(0, order_no.length - 1)
    var flag = that.data.order_no.substr(that.data.order_no.length - 1, 1)
    console.log("order_no",order_no)
    console.log("flag",flag)
    if (flag != 't' && flag != 'k' && flag != 'p' && flag != 'm') {
      e.error("参数错误")
      that.setData({
        wrong: true
      })
      return
    }

    that.setData({
      type: flag
    })

    e.get("Order/GetOrderDetail", {
       order_no: order_no,
       type: flag
      }, function (res) {
        console.log("order_info",res)

        if (res.code == 0) {
          if (res.info.complete) {
            e.alert('订单已核销')
            that.setData({
              finished: true,
              time: res.info.complete_time
            })
          }
          that.setData({
            detail: res.info,
            wrong: false
          })
        } else {
          e.alert(res.msg)
          that.setData({
            wrong: true
          })
        }
    })
  },

  /**
   * 确认核销
   */
  heXiao: function () {
      var that = this;

      if (that.data.finished) {
        e.alert("订单已核销");
        return
      }

      if (that.data.wrong) {
        e.alert("数据错误");
        return
      }

      wx.showModal({
        title: '提示',
        content: '确定核销吗？',
        success: function (ret) {
          if (ret.confirm) {
            e.loading("处理中...")
            e.get('Order/HeXiaoOrder',{
              order_id: that.data.detail.order_id,
              type: that.data.type,
              uid: t.getCache("userinfo").uid
            },function(res){
              e.hideLoading()

              if (res.code == 0) {
                e.success("核销成功");

                that.setData({
                  finished: true,
                  time: res.info.time
                })
              } else {
                console.log("hx_err_msg",res.msg)
                e.alert('核销失败！');
              }
            })
          }
        }
      })
  },

})